package com.smile;

import com.smile.config.entity.dto.LoginFrom;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SpringBootApplication
//@RestController
@Controller
@EnableJpaAuditing
public class SmileApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SmileApplication.class, args);
        SpringApplication app = new SpringApplication();
        app.setBannerMode(Banner.Mode.OFF);
        app.run(SmileApplication.class);
    }

    @GetMapping(value = {"/",""})
    public ModelAndView loginPage (HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
      }

    @GetMapping(value = {"/signup"})
    public ModelAndView signupPage (HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("signup");
        return modelAndView;
    }

    @GetMapping(value = {"/forgot"})
    public ModelAndView forgotPage (HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("forgot");
        return modelAndView;
    }

    @PostMapping(value = {"/authentication/form"})
    //@ResponseBody
    //public LoginFrom loginMethod ( LoginFrom loginFrom) {
    public ModelAndView loginMethod ( LoginFrom loginFrom) {

        ModelAndView modelAndView = new ModelAndView();

        if (!"admin".equals(loginFrom.getUsername())){
           modelAndView.setViewName("login");
        }
        else  {
          modelAndView.setViewName("redirect:/home");
        }

        return modelAndView;
    }

    @GetMapping("/test")
    @ResponseBody
    public String test (HttpServletRequest request, HttpServletResponse response) {

        return "test...";
    }
}
